<div class="row">
    <div class="alert alert-success" id="alert1" style="display:none;">
        <div id="msg1"></div>       
    </div>
    <div class="alert alert-warning" id="alert2" style="display:none;">
        <div id="msg2"></div>
    </div>
    <div class="alert alert-danger" id="alert3" style="display:none;">
        <div id="msg3"></div>
    </div>
</div>

<?php
//$inancm->pre($_REQUEST);
$upload_dir = '../images/blog/';
switch ($thisact){
	
	case 'add':
		include('modules/mod_blog/blog.add.php');
		break;
		
	
	case 'addsave':		
		//pre($_FILES);		
		
		if($_FILES["file"]["name"] != ""){	
			//Upload Image		
			$dt = date('YmdHis');			
			$file = $_FILES['file']['name'];
			$file_tmp = $_FILES["file"]["tmp_name"];			
			$file_type = $_FILES["file"]["type"];
			$file_size = $_FILES["file"]["size"];
								
			switch($file_type){
				case "image/pjpeg" :
				case "image/jpeg" :
				case "image/jpg" :
				case "image/gif":						
				case "image/png":
				case "image/x-png":						
				case "image/bmp":
					$isImage = true;
					break;
				default : 
					$isImage = false;
			}
			
			if ($isImage) {
				$copy = copy($file_tmp, "$upload_dir" .$dt."_".$file);   // Move Image From Temporary Location To Permanent Location
				
				if ($copy) {
					
					$filename = $dt."_".$file;	
					
					$Table_Name = 'inancm_blog';
					$data = array(
						'cat_id'=>$catid,
						'blog_image'=>$filename,
						'blog_title'=>$btitle,
						'blog_des'=>$bdes,
						'blog_detail'=>$bdetail,
						'blog_post'=>$bpost,
						'blog_tag'=>$btag,
						'blog_show'=>1						
					);
					
					$sql = $inancm->insert_db($Table_Name, $data);
					$rs = mysql_query($sql); 	
					
					
					if ($rs) {
						$message = "Save data successfully";
						?>
							<script type="text/javascript">			
							$(document).ready(function() {
								//alertPopup('msg1','alert1','<?=$message?>');
								alertPopup_goto('msg1','alert1','<?=$message?>','<?=$catid?>');
							});		
							</script>
						<?php
						//include('modules/mod_product/show.product.php');
					} else {
						$message = "Errors in save. Please try again.";
						?>
							<script type="text/javascript">			
							$(document).ready(function() {
								//alertPopup('msg3','alert3','<?=$message?>');
								alertPopup_goto('msg3','alert3','<?=$message?>','<?=$catid?>');
							});		
							</script>
						<?php
						//include('modules/mod_product/add.product.php');
					}
					
					
					
					
				} else { //Have problem copy
				
					$message = "Have problem upload file. Please try again.";
					?>
						<script type="text/javascript">			
						$(document).ready(function() {
							alertPopup('msg3','alert3','<?=$message?>');
						});		
						</script>
					<?php					
				}
				
				
			} else { //Is not image file
				
				$message = "This file is not a recommended image file format.";
				?>
					<script type="text/javascript">			
					$(document).ready(function() {
						alertPopup('msg3','alert3','<?=$message?>');
					});		
					</script>
				<?php
				
			}
		}

		break;
	
		
	/*====================================== DELETE ===========================================*/
	case 'delete':
		
		$sql_delete = "select * from inancm_blog where blog_id = '".$id."'";
		$data_delete = $inancm->select_db($sql_delete);		
		$blog_image = $data_delete[0]['blog_image'];		
											
		@unlink($upload_dir.''.$blog_image);
		
		
		$Table_Name = 'inancm_blog';
		$sql = $inancm->delete_db($Table_Name, array($Table_Name.'.blog_id='=>$id));
		$rs = mysql_query($sql);	
		//echo $sql;				

		
		if ($rs) {
			$message = "ลบข้อมูลเรียบร้อยแล้วค่ะ";
			?>
			<script type="text/javascript">			
			$(document).ready(function() {
				//alertPopup('msg1','alert1','<?=$message?>');
				alertPopup_goto('msg1','alert1','<?=$message?>','<?=$catid?>');
			});		
			</script>
			<?php
		} else {
			$message = "ผิดพลาดในการลบ กรุณาลองใหม่อีกครั้ง";
			?>
			<script type="text/javascript">			
			$(document).ready(function() {
				//alertPopup('msg3','alert3','<?=$message?>');
				alertPopup_goto('msg1','alert1','<?=$message?>','<?=$catid?>');
			});		
			</script>
			<?php
		}	
		break;	
		
		
		
	/*====================================== EDIT ===========================================*/	
	case 'edit':
		include('modules/mod_blog/blog.edit.php');
		break;	
		
		
	case 'editsave':
		$Table_Name = 'inancm_blog';
		$data = array(
			'cat_id'=>$catid,
			'blog_title'=>$btitle,
			'blog_des'=>$bdes,
			'blog_detail'=>$bdetail,
			'blog_post'=>$bpost,
			'blog_tag'=>$btag			
		);		
		$sql = $inancm->update_db($Table_Name, array('blog_id='=>$id), $data);
		$rs = mysql_query($sql); 	
		
		
		
		if($_FILES["file"]["name"] != ""){	
			//Upload Image		
			$dt = date('YmdHis');			
			$file = $_FILES['file']['name'];
			$file_tmp = $_FILES["file"]["tmp_name"];			
			$file_type = $_FILES["file"]["type"];
			$file_size = $_FILES["file"]["size"];
								
			switch($file_type){
				case "image/pjpeg" :
				case "image/jpeg" :
				case "image/jpg" :
				case "image/gif":						
				case "image/png":
				case "image/x-png":						
				case "image/bmp":
					$isImage = true;
					break;
				default : 
					$isImage = false;
			}
			
			if ($isImage) {
				$copy = copy($file_tmp, "$upload_dir" .$dt."_".$file);   // Move Image From Temporary Location To Permanent Location
				if ($copy) {
					
					$filename = $dt."_".$file;			
					
					@unlink($upload_dir.''.$oldimage);
		
					$data = array(
						'blog_image'=>$filename
					);
					
					$sql = $inancm->update_db($Table_Name, array('blog_id='=>$id), $data);
					$rs = mysql_query($sql); 	
					
					
					if ($rs) {
						$message = "Save data successfully";
						?>
							<script type="text/javascript">			
							$(document).ready(function() {
								alertPopup('msg1','alert1','<?=$message?>');
							});		
							</script>
						<?php
						//include('modules/mod_product/show.product.php');
					} else {
						$message = "Errors in save. Please try again.";
						?>
							<script type="text/javascript">			
							$(document).ready(function() {
								alertPopup('msg3','alert3','<?=$message?>');
							});		
							</script>
						<?php
						//include('modules/mod_product/add.product.php');
					}
					
					
					
					
				} else { //Have problem copy
				
					$message = "Have problem upload file. Please try again.";
					?>
						<script type="text/javascript">			
						$(document).ready(function() {
							alertPopup('msg3','alert3','<?=$message?>');
						});		
						</script>
					<?php					
				}
				
				
			} else { //Is not image file
				
				$message = "This file is not a recommended image file format.";
				?>
					<script type="text/javascript">			
					$(document).ready(function() {
						alertPopup('msg3','alert3','<?=$message?>');
					});		
					</script>
				<?php
				
			}
		} else {
		
			$message = "Save data successfully";
			?>
				<script type="text/javascript">			
				$(document).ready(function() {
					alertPopup_goto('msg1','alert1','<?=$message?>','<?=$catid?>');
				});		
				</script>
			<?php
		}

		
					
		break;	
		
		
	default:
		include('modules/mod_blog/blog.main.php');
}
?>


<script type="text/javascript">
function reloadPage(){
	window.location = 'index.php?ap=<?=$ap?>';
}	

var delayAlert=null; 
function alertPopup(msgid,alertid,message){
	$('#'+msgid+'').text(''+message+'');
	$('#'+alertid+'').fadeIn(500, function() {
		clearTimeout(delayAlert);  
		delayAlert=setTimeout(function(){  
			$('#'+alertid+'').fadeOut(1000); 
			reloadPage();  
			delayAlert=null;  
		},2000);  
	});
}


function alertPopup_goto(msgid,alertid,message,goto){
	$('#'+msgid+'').text(''+message+'');
	$('#'+alertid+'').fadeIn(500, function() {
		clearTimeout(delayAlert);  
		delayAlert=setTimeout(function(){  
			$('#'+alertid+'').fadeOut(1000); 
			window.location = 'index.php?ap=<?=$ap?>&catid='+goto+'';
			delayAlert=null;  
		},2000);  
	});
}



function checkData(id){
	if ($('#'+id+'').val() == ''){ 
		$('#'+id+'').closest('div').addClass("f_error");
		$('#'+id+'_err').fadeIn(1000);
		return 0;
	} else {
		$('#'+id+'').closest('div').removeClass("f_error");
		$('#'+id+'_err').fadeOut(100);
		return 1;
	}
}
</script>
